﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_CIB003
    {
        protected DA_CIB003 da = null;
        protected DA_CIB003 DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_CIB003();
                }
                return da;
            }
        }

        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }

        public string[] GetCriteriaArray()
        {
            string[] flds = DA.GetDistinctField();
            System.Collections.ArrayList newFld = new System.Collections.ArrayList();
            foreach (string str in flds)
            {
                newFld.Add(str);
            }
            newFld.Add("YYYYMM1");
            newFld.Add("YYYYMM2");
            newFld.Add("Season1");
            newFld.Add("Season2");
            newFld.Add("RM_GROUP");
            return (string[])newFld.ToArray(typeof(string));
        }

        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            //if (p_criteria.Count != 3)
            //{
            //    throw new Exception("Service 參數錯誤");
            //}
            DataTable dt = null;
            bool rmGroup = false;
            try
            {
                DA.ResetCriteria();
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().Equals("RM_GROUP"))
                    {
                        rmGroup = true;
                    }
                    DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());
                }

                bool p_checkAccount = (bool)p_criteria[1];
                string money = (string)p_criteria[2];
                dt = DA.GetSummaryData(p_checkAccount, money,rmGroup);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable GetSummaryData2(System.Collections.ArrayList p_criteria)
        {
            if (p_criteria.Count != 4)
            {
                throw new Exception("Service 參數錯誤");
            }
            DataTable dt = null;
            try
            {
                string[] YM = {string.Empty,string.Empty};
                string[] YMCollect = null;
                //string[] fieldsAS = { string.Empty, string.Empty };
                string[] fieldsAS = null;
                System.Collections.ArrayList newYM = new System.Collections.ArrayList();
                DA.ResetCriteria();
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                bool rmGroup = false;
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().Equals("YYYYMM"))
                    {
                        YM = SL.GetListArray();
                        //if (YM[0].ToString().IndexOf(',') > 0)
                        if (YM[0].ToString().IndexOf('Q') > 0)
                        {
                            //YMCollect = (YM[0].ToString() + YM[1].ToString()).Split(',');
                            fieldsAS = YM;
                            YMCollect = season2Months(YM);
                            DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), YMCollect);
                            YM = YMCollect;

                            List<string> lst = new List<string>();
                            lst.Add((string)p_criteria[2]);
                            seasonDays(YMCollect,lst);
                            lst.Add(fieldsAS[0].ToString());
                            lst.Add(fieldsAS[1].ToString());
                            //getDays(YMCollect.GetValue(0).ToString());
                            /*
                           * {0} 最小金額EX:1億             
                           * {1}{3}{5} 第一個季中的三個月份 
                           * {2}{4}{6} 第一個季中月份的實際天數
                           * {7} 第一個季中月份的總天數             
                           * {8}{10}{12} 第二個季中的三個月份
                           * {9}{11}{13} 第二個季中月份的實際天數			              
                           * {14} 第二個季中的總天數			 
                           * {15}      EX:YYYYQ1 
                           * {16}      EX:YYYYQ2
                           */
                            fieldsAS = lst.ToArray();
                        }
                        else
                        {
                            fieldsAS = SL.GetListArray();
                            DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());
                        }

                    }
                    else
                    {
                        if (SL.GetField().Equals("RM_GROUP"))
                        {
                            rmGroup = true;
                        }
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());
                    }                    
                }

                bool p_checkAccount = (bool)p_criteria[1];
                string sign = (string)p_criteria[3];
                string money = (string)p_criteria[2];
                dt = DA.GetSummaryData2(p_checkAccount, money,YM,fieldsAS,sign,rmGroup);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable GetSummaryData3(System.Collections.ArrayList p_criteria)
        {
            if (p_criteria.Count != 4)
            {
                throw new Exception("Service 參數錯誤");
            }
            DataTable dt = null;
            try
            {
                string[] YM = { string.Empty, string.Empty };
                System.Collections.ArrayList newYM = new System.Collections.ArrayList();
                DA.ResetCriteria();
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                bool rmGroup = false;
                string[] rmList = null;
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().Equals("YYYYMM"))
                    {
                        YM = Utility.GetAllMonths(SL.GetListArray());
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), YM);
                    }
                    else if(SL.GetField().Equals("RM_GROUP"))
                    {
                        rmGroup = true;
                        rmList = SL.GetListArray();
                    }
                    else
                    {
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());                    
                    }
                }
                
                bool p_checkAccount = (bool)p_criteria[1];
                string sign = (string)p_criteria[3];
                string money = (string)p_criteria[2];
                dt = DA.GetSummaryData3(p_checkAccount, money, YM, sign
                    , rmGroup,rmList);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable GetDetailData(System.Collections.ArrayList p_criteria)
        {
            //if (p_criteria.Count != 4)
            //{
            //    throw new Exception("Service 參數錯誤");
            //}

            DataTable dt = null;
            try
            {
                string lanc_id, yyyymm, accountno,money;
                lanc_id = Convert.ToString(p_criteria[0]);
                yyyymm = Convert.ToString(p_criteria[1]);
                accountno = Convert.ToString(p_criteria[2]);
                money = Convert.ToString(p_criteria[3]);
                string sign = Convert.ToString(p_criteria[4]);
                List<iSelectList> criteriaList = p_criteria[5] as List<iSelectList>;
                DA.ResetCriteria();
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().Equals("CUR"))
                    {
                        string[] fieldsAS =  SL.GetListArray();
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());
                    }
                }
                dt = DA.GetDetailData(lanc_id, yyyymm, accountno,money,sign);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return dt;
        }

        protected string[] season2Months(string[] seasons)
        {
            StringBuilder sb = new StringBuilder();
            string months;
            months = string.Empty;
            foreach (string Q in seasons)
            {
                switch(Q.Substring(5, 1))
                {
                    case "1":
                        months = ",{0}01,{0}02,{0}03";
                        break;
                    case "2":
                        months = ",{0}04,{0}05,{0}06";
                        break;
                    case "3":
                        months = ",{0}07,{0}08,{0}09";
                        break;
                    case "4":
                        months = ",{0}10,{0}11,{0}12";
                        break;                
                }
                sb.AppendFormat(months, Q.Substring(0, 4));
            }
            return sb.ToString().Substring(1).Split(new char[] { ',' });
        }

        protected int getDays(string p_ym)
        {
            string ym = string.Format("{0}-{1}-01"
                , p_ym.Substring(0, 4), p_ym.Substring(4, 2));
            DateTime date = Convert.ToDateTime(ym).AddMonths(1).AddDays(-1);
            return date.Day;
        }

        /// <summary>
        /// 算出四季中的當月天數和當季的總天數
        /// </summary>
        /// <param name="p_ymList"></param>
        /// <returns></returns>
        protected void seasonDays(string[] p_ymList,List<string> p_lst)
        {
            int i,totalDays,days;
            i= 1;
            totalDays = 0;
            days = 0;
            foreach (string ym in p_ymList)
            {

                days = getDays(ym);
                totalDays += days;
                p_lst.Add(ym);
                p_lst.Add(days.ToString());
                if (i.Equals(3))
                {
                    i = 0;
                    p_lst.Add(totalDays.ToString());
                    totalDays = 0;
                }
                i++;
            }
        }
    }
}
